/**
 * Created with IntelliJ IDEA.
 * User: Thoughtworker
 * Date: 8/21/13
 * Time: 1:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class StringCalculator {
    public Integer add(String stringOfNumbers) {
        String addendArray = stringOfNumbers;
        String delimiters = "\\n|\\,";

        if(stringOfNumbers.contains("//")){
            delimiters += ("|" + getCustomDelimiter(stringOfNumbers));
            addendArray = removeCustomDelimiterDeclaration(stringOfNumbers);
        }

        int result = 0;

        for(String addendString : addendArray.split(delimiters)){
            if (addendString == "" || Integer.parseInt(addendString) > 1000){
                result += 0;
            }   else{
                result += Integer.parseInt(addendString);
            }
        }

        return result;

    }

    public String getCustomDelimiter(String stringOfNumbers) {
        String customDelimiter = stringOfNumbers.substring(3,stringOfNumbers.indexOf("]"));
        return customDelimiter;
    }

    public String removeCustomDelimiterDeclaration(String stringOfNumbers) {
        return stringOfNumbers.substring(stringOfNumbers.indexOf("]") + 2);
    }
}
